package controller;

import com.google.gson.Gson;
import model.Suppliecharacter;
import services.SuppliecharacterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user4")//访问接口的名字
public class SuppliecharacterController extends HttpServlet {
    private static Gson gson=new Gson();//第三方插件，返回gson格式的插件

    @Override//重写
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//请求 响应  针对的是前端页面的get方法
        //用户所做的操作
        String oper = req.getParameter("oper");//获取地址栏当中oper后边的值   请求对象实例化
        System.out.println("获取地址栏当中的数据为：" + oper);//能否获取地址栏中的值
        //suppliecharacter表的数据
        /*String name1=req.getParameter("name1");
        String sex=req.getParameter("sex");
        String phone=req.getParameter("phone");
        String limit1=req.getParameter("limit1");
        String fromsupplier=req.getParameter("fromsupplier");
        String managername=req.getParameter("managername");
        String state=req.getParameter("state");
        String creater=req.getParameter("creater");
        *//*String createtime=req.getParameter("createtime");*//*
        String updater=req.getParameter("updater");
       *//* String updatetime=req.getParameter("updatetime");*//*
         *//*Integer userId = Integer.getInteger(req.getParameter("userid"));*//*
        String password1=req.getParameter("password1");
        String email=req.getParameter("email");
        String workplace=req.getParameter("workplace");
        String servicearea=req.getParameter("servicearea");
        String duty=req.getParameter("duty");
        String educationbackground=req.getParameter("educationbackground");
        String personid=req.getParameter("personid");
        String remark=req.getParameter("remark");
*/

        Suppliecharacter suppliecharacter = new Suppliecharacter();////suppliecharacter表实例化

        /*suppliecharacter.setName1(name1);
        suppliecharacter.setSex(sex);//调用set方法拿到给各字段设置的值
        suppliecharacter.setPhone(phone);
        suppliecharacter.setLimit1(limit1);
        suppliecharacter.setFromsupplier(fromsupplier);
        suppliecharacter.setManagername(managername);
        suppliecharacter.setState(state);
        suppliecharacter.setCreater(creater);
        *//*suppliecharacter.setCreatetime(createtime);*//*
        suppliecharacter.setUpdater(updater);
        *//*suppliecharacter.setUpdatetime(updatetime);*//*
         *//* suppliecharacter.setUserId(userId);*//*
        suppliecharacter.setPassword1(password1);
        suppliecharacter.setEmail(email);
        suppliecharacter.setWorkplace(workplace);
        suppliecharacter.setServicearea(servicearea);
        suppliecharacter.setDuty(duty);
        suppliecharacter.setEducationbackground(educationbackground);
        suppliecharacter.setPersonid(personid);
        suppliecharacter.setRemark(remark);
*/

        List<Suppliecharacter> tempReturn1 = null;//返回集合操作结果  （查询时候使用）没有new的还要进行实例化


        int tempReturn = 0;//返回int操作结果  （增删改时使用）
        SuppliecharacterService suppliecharacterService = new SuppliecharacterService();//实例化业务功能类

        if (oper != null) {//判断地址栏输入的oper不等于空    选择要做的操作
            if (req.getParameter("suppliecharacterid") != null) {
                String suppliecharacterid = req.getParameter("suppliecharacterid");
                System.out.println("获得地址栏中表的数据为" + suppliecharacterid);
            }
            if (req.getParameter("name1") != null) {
                String name1 = req.getParameter("name1");
                System.out.println("获得地址栏中表的数据为" + name1);
                suppliecharacter.setName1(name1);
            }
            if (req.getParameter("sex") != null) {
                String sex = req.getParameter("sex");
                System.out.println("获得地址栏中表的数据为" + sex);
                suppliecharacter.setSex(sex);
            }
            if (req.getParameter("phone") != null) {
                String phone = req.getParameter("phone");
                System.out.println("获得地址栏中表的数据为" + phone);
                suppliecharacter.setPhone(phone);
            }

            if (req.getParameter("limit1") != null) {
                String limit1 = req.getParameter("limit1");
                System.out.println("获得地址栏中表的数据为" + limit1);
                suppliecharacter.setLimit1(limit1);
            }
            if (req.getParameter("fromsupplier") != null) {
                String fromsupplier = req.getParameter("fromsupplier");
                System.out.println("获得地址栏中表的数据为" + fromsupplier);
                suppliecharacter.setFromsupplier(fromsupplier);
            }
            if (req.getParameter("managername") != null) {
                String managername = req.getParameter("managername");
                System.out.println("获得地址栏中表的数据为" + managername);
                suppliecharacter.setManagername(managername);
            }
            if (req.getParameter("state") != null) {
                String state = req.getParameter("state");
                System.out.println("获得地址栏中表的数据为" + state);
                suppliecharacter.setState(state);
            }
            if (req.getParameter("creater") != null) {
                String creater = req.getParameter("creater");
                System.out.println("获得地址栏中表的数据为" + creater);
                suppliecharacter.setCreater(creater);
            }
            if (req.getParameter("createtime") != null) {
                String createtime = req.getParameter("createtime");
                System.out.println("获得地址栏中表的数据为" + createtime);
                suppliecharacter.setCreatetime(createtime);
            }
            if (req.getParameter("updater") != null) {
                String updater = req.getParameter("updater");
                System.out.println("获得地址栏中表的数据为" + updater);
                suppliecharacter.setUpdater(updater);
            }
            if (req.getParameter("updatetime") != null) {
                String updatetime = req.getParameter("updatetime");
                System.out.println("获得地址栏中表的数据为" + updatetime);
                suppliecharacter.setUpdatetime(updatetime);
            }
            if (req.getParameter("userId") != null) {
                String userId = req.getParameter("userId");
                System.out.println("获得地址栏中表的数据为" + userId);
                suppliecharacter.setUserId(userId);
            }
            if (req.getParameter("password1") != null) {
                String password1 = req.getParameter("password1");
                System.out.println("获得地址栏中表的数据为" + password1);
                suppliecharacter.setPassword1(password1);
            }
            if (req.getParameter("email") != null) {
                String email = req.getParameter("email");
                System.out.println("获得地址栏中表的数据为" + email);
                suppliecharacter.setEmail(email);
            }
            if (req.getParameter("workplace") != null) {
                String workplace = req.getParameter("workplace");
                System.out.println("获得地址栏中表的数据为" + workplace);
                suppliecharacter.setWorkplace(workplace);
            }
            if (req.getParameter("servicearea") != null) {
                String servicearea = req.getParameter("servicearea");
                System.out.println("获得地址栏中表的数据为" + servicearea);
                suppliecharacter.setServicearea(servicearea);
            }
            if (req.getParameter("duty") != null) {
                String duty = req.getParameter("duty");
                System.out.println("获得地址栏中表的数据为" + duty);
                suppliecharacter.setDuty(duty);
            }
            if (req.getParameter("educationbackground") != null) {
                String educationbackground = req.getParameter("educationbackground");
                System.out.println("获得地址栏中表的数据为" + educationbackground);
                suppliecharacter.setEducationbackground(educationbackground);
            }
            if (req.getParameter("personid") != null) {
                String personid = req.getParameter("personid");
                System.out.println("获得地址栏中表的数据为" + personid);
                suppliecharacter.setPersonid(personid);
            }
            if (req.getParameter("remark") != null) {
                String remark = req.getParameter("remark");
                System.out.println("获得地址栏中表的数据为" + remark);
                suppliecharacter.setRemark(remark);
            }
            //操作的内容
            switch (oper) {
                case "1":
                    //查询
                    //单项查询
                    tempReturn1 = suppliecharacterService.getDan(suppliecharacter);//调用service层的getall方法 返回查询结果
                    break;
                case "2":
                    //添加
                    tempReturn = suppliecharacterService.add(suppliecharacter);//调用service层的add方法  返回添加结果
                    if (tempReturn == 0) {
                        System.out.println("失败" + tempReturn);
                    } else {
                        System.out.println("成功" + tempReturn);
                    }
                    break;
                case "3":
                    //修改
                    String suppliecharacterid = req.getParameter("suppliecharacterid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    suppliecharacter.setSuppliecharacterid(Integer.parseInt(suppliecharacterid));//强制类型转换  给实体主键赋值
                    System.out.println(suppliecharacter);
                    String name1 = req.getParameter("name1");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    suppliecharacter.setName1(name1);
                    tempReturn = suppliecharacterService.update(suppliecharacter);//调用service层的update方法  返回修改结果
                    break;
                case "4":
                    //删除
                    String suppliecharacterid1 = req.getParameter("suppliecharacterid");//主键  删除时必须获取表中主键（地址栏中必须有该参数）
                    if (suppliecharacterid1 != null) {
                        suppliecharacter.setSuppliecharacterid(Integer.parseInt(suppliecharacterid1));//强制类型转换  给实体主键赋值
                        System.out.println(suppliecharacterid1);
                        tempReturn = suppliecharacterService.del(suppliecharacter);//调用service层的del方法  返回删除结果

                    }
                    break;
                case "5":
                    //查询全部
                    tempReturn1 = suppliecharacterService.getAll(suppliecharacter);//调用service层的getall方法 返回查询结果
                    break;
                case "6":
                    //更改方法按id查询
                    String suppliecharacterid2 = req.getParameter("suppliecharacterid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    suppliecharacter.setSuppliecharacterid(Integer.parseInt(suppliecharacterid2));//强制类型转换  给实体主键赋值
                    tempReturn1 = suppliecharacterService.getAddc(suppliecharacter);//调用service层的getall方法 返回查询结果
                    break;
            }

            //设置返回信息


            //将所有的返回值都写入到map当中
            Map map = new HashMap();//
            map.put("code", "1");//返回编号：1：成功；0：失败；-1：用户id重复
            map.put("statue", "success");
            if (tempReturn1 != null) {
                map.put("results", tempReturn1);//成功的结果 操作的结果
            } else {
                map.put("results", tempReturn);
            }
            //将数据转换为gson格式进行发送
            String rs = gson.toJson(map);
            //将数据发送后并进行打印
            System.out.println(rs+"---------"+map);
            resp.getWriter().print(rs);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{//请求  响应  针对的是前端页面的post方法
        super.doGet(req,resp);//调用父类方法
    }
}
